<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="书籍名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入书籍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入书籍作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍封面" prop="img">
        <el-input
          v-model="queryParams.img"
          placeholder="请输入书籍封面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍分类id" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入书籍分类id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍是否免费" prop="free">
        <el-input
          v-model="queryParams.free"
          placeholder="请输入书籍是否免费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览量" prop="pageViews">
        <el-input
          v-model="queryParams.pageViews"
          placeholder="请输入浏览量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍价格" prop="bookPrice">
        <el-input
          v-model="queryParams.bookPrice"
          placeholder="请输入书籍价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍章节数" prop="chapterCount">
        <el-input
          v-model="queryParams.chapterCount"
          placeholder="请输入书籍章节数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书籍总字数" prop="wordCount">
        <el-input
          v-model="queryParams.wordCount"
          placeholder="请输入书籍总字数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yljf:book:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yljf:book:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yljf:book:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yljf:book:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="书籍主键" align="center" prop="id" />
      <el-table-column width="160" label="书籍名称" align="center" prop="name" />
      <el-table-column label="书籍作者" align="center" prop="author" />
      <el-table-column label="书籍封面" align="center" prop="img" />
      <el-table-column width="100" label="书籍分类id" align="center" prop="category" />
      <el-table-column label="书籍是否免费" width="150" align="center" prop="free">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.free"/>
        </template>
      </el-table-column>
      <el-table-column width="160" label="内容简介" align="center" prop="synopsis" >
        <template slot-scope="scope">
          <div class="cell-content">{{scope.row.synopsis}}</div>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="pageViews" />
      <el-table-column label="书籍价格" align="center" prop="bookPrice" />
      <el-table-column label="书籍章节数" align="center" prop="chapterCount" />
      <el-table-column label="书籍总字数" align="center" prop="wordCount" />
      <el-table-column label="书籍评论数" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yljf:book:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yljf:book:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改书籍对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="书籍名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入书籍名称" />
        </el-form-item>
        <el-form-item label="书籍作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入书籍作者" />
        </el-form-item>
        <el-form-item label="书籍封面" prop="img">
          <el-input v-model="form.img" placeholder="请输入书籍封面" />
        </el-form-item>
        <el-form-item label="书籍分类id" prop="category">
          <el-input v-model="form.category" placeholder="请输入书籍分类id" />
        </el-form-item>
        <el-form-item label="书籍是否免费" prop="free">
          <el-input v-model="form.free" placeholder="请输入书籍是否免费" />
        </el-form-item>
        <el-form-item label="内容简介" prop="synopsis">
          <el-input v-model="form.synopsis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="浏览量" prop="pageViews">
          <el-input v-model="form.pageViews" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="书籍价格" prop="bookPrice">
          <el-input v-model="form.bookPrice" placeholder="请输入书籍价格" />
        </el-form-item>
        <el-form-item label="书籍章节数" prop="chapterCount">
          <el-input v-model="form.chapterCount" placeholder="请输入书籍章节数" />
        </el-form-item>
        <el-form-item label="书籍总字数" prop="wordCount">
          <el-input v-model="form.wordCount" placeholder="请输入书籍总字数" />
        </el-form-item>
        <el-form-item label="${comment}" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="书籍评论数" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入书籍评论数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBook, getBook, delBook, addBook, updateBook } from "@/api/yljf/book";

export default {
  name: "Book",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 书籍表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        author: null,
        img: null,
        category: null,
        free: null,
        synopsis: null,
        pageViews: null,
        bookPrice: null,
        chapterCount: null,
        wordCount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询书籍列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        author: null,
        img: null,
        category: null,
        free: null,
        synopsis: null,
        pageViews: null,
        bookPrice: null,
        chapterCount: null,
        wordCount: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加书籍";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBook(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改书籍";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除书籍编号为"' + ids + '"的数据项？').then(function() {
        return delBook(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yljf/book/export', {
        ...this.queryParams
      }, `book_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style lang="scss" scoped>
  .cell-content{
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3; /* 限制行数为3 */
      overflow: hidden;
  }

  ::v-deep{
    .el-form-item__label{
      width: initial !important;
    }
  }
</style>
